package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotUpkeepTemplate;
import org.dromara.iot.domain.bo.IotUpkeepProjectBo;
import org.dromara.iot.domain.bo.IotUpkeepTemplateBo;
import org.dromara.iot.domain.vo.IotUpkeepProjectVo;
import org.dromara.iot.domain.vo.IotUpkeepTemplateVo;
import org.dromara.iot.mapper.IotUpkeepTemplateMapper;
import org.dromara.iot.service.IIotUpkeepProjectService;
import org.dromara.iot.service.IIotUpkeepTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 电梯保养模板Service业务层处理
 *
 * @author lsl
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class IotUpkeepTemplateServiceImpl implements IIotUpkeepTemplateService {

    private final IotUpkeepTemplateMapper baseMapper;

    private final IIotUpkeepProjectService projectService;

    /**
     * 查询电梯保养模板
     */
    @Override
    public IotUpkeepTemplateVo queryById(Long templateId) {
        IotUpkeepTemplateVo iotUpkeepTemplateVo = baseMapper.selectVoById(templateId);
        List<IotUpkeepProjectVo> iotUpkeepProjectVos = projectService.queryListByTemplateId(templateId);
        iotUpkeepTemplateVo.setUpkeepProjectList(iotUpkeepProjectVos);
        return iotUpkeepTemplateVo;
    }

    /**
     * 查询电梯保养模板列表
     */
    @Override
    public TableDataInfo<IotUpkeepTemplateVo> queryPageList(IotUpkeepTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotUpkeepTemplate> lqw = buildQueryWrapper(bo);
        Page<IotUpkeepTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯保养模板列表
     */
    @Override
    public List<IotUpkeepTemplateVo> queryList(IotUpkeepTemplateBo bo) {
        LambdaQueryWrapper<IotUpkeepTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotUpkeepTemplate> buildQueryWrapper(IotUpkeepTemplateBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotUpkeepTemplate> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, IotUpkeepTemplate::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), IotUpkeepTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(StringUtils.isNotBlank(bo.getElevatorType()), IotUpkeepTemplate::getElevatorType, bo.getElevatorType());
        lqw.eq(StringUtils.isNotBlank(bo.getUpkeepType()), IotUpkeepTemplate::getUpkeepType, bo.getUpkeepType());
        return lqw;
    }

    /**
     * 新增电梯保养模板
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insertByBo(IotUpkeepTemplateBo bo) {
        IotUpkeepTemplate add = MapstructUtils.convert(bo, IotUpkeepTemplate.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTemplateId(add.getTemplateId());
            List<IotUpkeepProjectBo> upkeepProjectList = bo.getUpkeepProjectList();
            upkeepProjectList.forEach(upkeepProjectBo -> upkeepProjectBo.setTemplateId(add.getTemplateId()));
            projectService.insertBatchByBo(upkeepProjectList);
        }
        return flag;
    }

    /**
     * 修改电梯保养模板
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateByBo(IotUpkeepTemplateBo bo) {
        IotUpkeepTemplate update = MapstructUtils.convert(bo, IotUpkeepTemplate.class);
        validEntityBeforeSave(update);
        projectService.deleteWithValidByTemplateIds(Collections.singleton(bo.getTemplateId()));
        projectService.insertBatchByBo(bo.getUpkeepProjectList());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotUpkeepTemplate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯保养模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        projectService.deleteWithValidByTemplateIds(ids);
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
